<?php

namespace Xrfg\Api\Trading\Spot\Account\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\Auth;
use Xrfg\Api\Trading\Spot\Account\Requests\SpotAccountCapitalflowRequest;
use Xrfg\Api\Trading\Spot\Account\Resources\SpotAccountCapitalflowResource;
use Xrfg\Core\Trading\Account\Models\Transfer;
use Xrfg\Core\Trading\Spot\Models\Order;
use Xrfg\Core\Trading\Spot\Models\SpotAccountCapitalflow;
use Xrfg\Core\Trading\Spot\Models\Trade;

class SpotAccountCapitalflowController extends Controller
{
    /**
     * 币币账户资金流水.
     *
     * @param SpotAccountCapitalflowRequest $request
     *
     * @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection
     */
    public function index(SpotAccountCapitalflowRequest $request)
    {
        $coinId = $request->query('coin_id');
        $type = $request->query('type');
        $start = $request->query('start_time');
        $end = $request->query('end_time');
        $perPage = $request->query('per_page', 15);
        $perPage = $perPage > 100 ? 100 : $perPage;

        $spotAccountCapitalflows = SpotAccountCapitalflow::where('user_id', Auth::id())
            ->when(null !== $coinId, function (Builder $query) use ($coinId) {
                return $query->where('coin_id', $coinId);
            })
            ->when(null !== $type, function (Builder $query) use ($type) {
                switch ($type) {
                    case 1: // 转入
                        return $query->where('class_id', Transfer::CLASS_ID)
                            ->where('op_id', Transfer::OP_SPOT_ACCOUNT_IN);
                        break;
                    case 2: // 转出
                        return $query->where('class_id', Transfer::CLASS_ID)
                            ->where('op_id', Transfer::OP_SPOT_ACCOUNT_OUT);
                        break;
                    case 3: // 买入挂单
                        return $query->where('class_id', Order::CLASS_ID)
                            ->where('op_id', Order::OP_BUY_NEW);
                        break;
                    case 4: // 买入撤销
                        return $query->where('class_id', Order::CLASS_ID)
                            ->where('op_id', Order::OP_BUY_CANCELED);
                        break;
                    case 5: // 卖出挂单
                        return $query->where('class_id', Order::CLASS_ID)
                            ->where('op_id', Order::OP_SELL_NEW);
                        break;
                    case 6: // 卖出撤销
                        return $query->where('class_id', Order::CLASS_ID)
                            ->where('op_id', Order::OP_SELL_CANCELED);
                        break;
                    case 7: // 买入收入
                        return $query->where('class_id', Trade::CLASS_ID)
                            ->where('op_id', Trade::OP_BUY_DEAL_IN);
                        break;
                    case 8: // 买入支出
                        return $query->where('class_id', Trade::CLASS_ID)
                            ->where('op_id', Trade::OP_BUY_DEAL_OUT);
                        break;
                    case 9: // 卖出收入
                        return $query->where('class_id', Trade::CLASS_ID)
                            ->where('op_id', Trade::OP_SELL_DEAL_IN);
                        break;
                    case 10: // 卖出支出
                        return $query->where('class_id', Trade::CLASS_ID)
                            ->where('op_id', Trade::OP_SELL_DEAL_OUT);
                        break;
                    case 11: // 差价返还
                        return $query->where('class_id', Trade::CLASS_ID)
                            ->where('op_id', Trade::OP_DEAL_SPREAD);
                        break;
                    default:
                        break;
                }
            })
            ->when(null !== $start, function (Builder $query) use ($start) {
                return $query->where('created_at', '>=', $start);
            })
            ->when(null !== $end, function (Builder $query) use ($end) {
                return $query->where('created_at', '<=', $end);
            })
            ->orderByDesc('id')
            ->paginate($perPage)
            ->appends($request->only(['coin_id', 'type', 'start_time', 'end_time', 'per_page']));

        return SpotAccountCapitalflowResource::collection($spotAccountCapitalflows);
    }
}
